auto.waitFor();
//京东签到+升级赚豆

var myFunc = require("./myCommonFunction.js");
var storage = myFunc.initDateStorage();

threads.start(function () {
    sleep(420000)
    exit();
});
// liulan2()
// liulan()
opreate();

exit();
function opreate() {
    var ret = myFunc.initNetWork(null, true);
    if (ret) {
        // var shengying = device.getMusicVolume();
        // device.setMusicVolume(0);
        jingdong();
        // device.setMusicVolume(shengying);
    }
}

function jingdong() {
    try {
        myFunc.openMyApp("京东");
        var a = text("秒杀").findOne(5000);
        if (a == null) {
            myFunc.tuichu("京东");
            jingdong();
        } else {
            myFunc.pClick(a)
            textContains("签到日历").findOne(5000);
            sleep(1000);
            myFunc.clickByTextC("签到领豆", 1000, 1);
            //升级赚京豆
            liulan();
            // click(765, 754)
            // sleep(1000);
            myFunc.tuichu("京东");
        }
    } catch (error) {
        log(error);
        myFunc.closeAppEx("京东");
        jingdong();
    }
}


function liulan() {
    if (storage.contains('升级赚京豆')) {
        return;
    }
    myFunc.log("升级赚京豆");
    // click(694, 438);
    let a = myFunc.findOne("连签", 3000, 1);
    myFunc.clickCenter(a, 1, 0, 80)
    sleep(1000);
    liulan2();
    if(myFunc.findOnce('去完成', 1) == null && myFunc.findOnce('签到日历', 1) != null) {
        storage.set("升级赚京豆", 1);
    } else {
        throw "升级赚京豆失败";
    }

}
function liulan2() {
    let a = myFunc.findOne('去完成', 3000, 1);
    while(a != null) {
        myFunc.clickCenter(a, 1, 0, -250)
        sleep(7000);
        myFunc.tuichuToStr('浏览5s', 1000, 1, 1);
        sleep(1000);
        a = myFunc.findOnce('去完成', 1);
    }
}

function liulan1() {
    if (textContains("去完成").find().length <= 0) {
        return;
    }
    var task = textContains("/").textMatches(".*\\d{1,2}/\\d{1,2}\\)").find();
    var task1 = task[0];
    var desc = className(task1.className()).indexInParent(task1.indexInParent()).depth(task1.depth() + 1).find();
    // var status = className("android.view.ViewGroup").indexInParent(task1.indexInParent()).depth(task1.depth() - 1).find();
    myFunc.log(task.length, desc.length, 2);
    var word = ['京豆可抵'];
    let dica = desc.length - task.length;
    for (let index = 0; index < task.length; index++) {
        var descText = desc[index + dica].text();
        var taskText = task[index].text();
        myFunc.log(taskText, descText, "描述");
    
        if (taskText.indexOf("券") != -1) {
            continue;
        }
        // if (descText.indexOf("关注") != -1) {
        //     continue;
        // }
        var numIdx = myFunc.getNumLimit(taskText);
        if (numIdx[0] == numIdx[1]) {
            continue;
        }
        var ts = text(taskText).findOnce();

        if (taskText.indexOf(".9元") != -1) {
            myFunc.clickCenter(ts);
            sleep(100);
            back();
            sleep(1000);
        }
        if (taskText.indexOf("免费水果") != -1) {
            myFunc.clickCenter(ts);
            if(!myFunc.clickByText('去完成任务')) {
                sleep(2000);
            }
            sleep(2000);
            myFunc.clickByText('免费水果');
            sleep(5000);
            myFunc.clickTextByBtn('去领取');
            myFunc.clickTextByBtn('收下水滴');
            sleep(1000);
            back();
            jingdong();
            return;
        }
        var sleepTime = 1500;
        var idx = descText.match('\\d*s');
        if (idx != null && idx.length > 0) {
            sleepTime = idx[0].substring(0, idx[0].length-1) * 1000 + 2000;
            ts.className();
        } 
        for (let y = numIdx[0]; y < numIdx[1]; y++) {
            myFunc.clickCenter(ts);
            sleep(sleepTime);
            myFunc.tuichuToStr(word);
            sleep(1000);
        }
        liulan1();
        break;
    }
}










